﻿using System.IO;
using System.Web.UI;
using SuperSimpleWebshop.Common;
using SuperSimpleWebshop.Domain;
using SuperSimpleWebshop.Domain.Interfaces;
using umbraco;
using SuperSimpleWebshop.Domain.Helpers;
using umbraco.BusinessLogic;
using SuperSimpleWebshop.Domain.Repositories;

namespace SuperSimpleWebshop.Payment.SagePay
{
    public class SagePayPaymentResultHandler : IPaymentResultHandler
    {
        #region IPaymentResultHandler Members
        /// <summary>
        /// Gets the payment provider name
        /// </summary>
        /// <returns>Name of the payment provider</returns>
        public string GetName()
        {
            return "SagePay";
        }

        /// <summary>
        /// Gets the HTML to show to the customer
        /// </summary>
        /// <param name="order">Order of the payment</param>
        /// <returns>Html with payment result</returns>
        public string GetResultHtml(Order order)
        {
            // Identify Order
            string transactionId = library.Request("VPSTxId");

            var stringWriter = new StringWriter();
            var htmlWriter = new HtmlTextWriter(stringWriter);

            if (order != null)
            {

                htmlWriter.WriteBeginTag("div");
                htmlWriter.WriteAttribute("class", "paymentmessage");
                htmlWriter.Write(HtmlTextWriter.TagRightChar);

                if (order.OrderInfo.Status == OrderStatus.Ready_for_Dispatch)
                {
                    htmlWriter.RenderBeginTag("h3");
                    htmlWriter.Write(library.GetDictionaryItem("PaymentSuccess"));
                    htmlWriter.RenderEndTag();

                    htmlWriter.RenderBeginTag("p");
                    htmlWriter.Write(library.GetDictionaryItem("PaymentThankYou"));
                    htmlWriter.RenderEndTag();

                    htmlWriter.WriteEndTag("div");

                    return stringWriter.ToString();
                }

                if (order.OrderInfo.Status == OrderStatus.Payment_Failed)
                {
                    htmlWriter.RenderBeginTag("h3");
                    htmlWriter.Write(library.GetDictionaryItem("PaymentFailed"));
                    htmlWriter.RenderEndTag();

                    htmlWriter.WriteEndTag("div");

                    return stringWriter.ToString();
                }

                if (order.OrderInfo.Status == OrderStatus.Waiting_for_Payment)
                {
                    htmlWriter.RenderBeginTag("h3");
                    htmlWriter.Write(library.GetDictionaryItem("PaymentPending"));
                    htmlWriter.RenderEndTag();

                    htmlWriter.WriteEndTag("div");

                    return stringWriter.ToString();
                }
            }
            else
            {
                return "Order not found";
            }

            return string.Empty;
        }
        #endregion
    }
}